<template>
  <div class="product-card">
    <!-- 商品名称 -->
    <div class="product-name">{{ product.productName }}</div>
    
    <!-- 商品基本信息 -->
    <div class="product-meta">
      <div class="meta-row">
        <span class="meta-label">供应商</span>
        <span class="meta-value">{{ product.supplier }}</span>
      </div>
      <div class="meta-row">
        <span class="meta-label">批次码</span>
        <span class="meta-value">{{ product['6892d3849f73713c5799ff20'] || '-' }}</span>
      </div>
      <div class="meta-row">
        <span class="meta-label">库位编码</span>
        <span class="meta-value">{{ product.locationCode }}</span>
      </div>
      <div class="meta-row">
        <span class="meta-label">商品条码</span>
        <span class="meta-value">{{ product.barcode }}</span>
      </div>
      <div class="meta-row" v-if="product.productionDate">
        <span class="meta-label">生产日期</span>
        <span class="meta-value">{{ formatDate(product.productionDate) }}</span>
      </div>
    </div>

    <!-- 库存统计 -->
    <div class="stock-stats">
      <div class="stat-item available">
        <span class="stat-label">可用库存</span>
        <span class="stat-number">{{ product.availableStock }}</span>
      </div>
      <div class="stat-item locked" v-if="product.lockedStock > 0">
        <span class="stat-label">锁定库存</span>
        <span class="stat-number">{{ product.lockedStock }}</span>
      </div>
      <div class="stat-item total">
        <span class="stat-label">总库存</span>
        <span class="stat-number">{{ product.totalStock }}</span>
      </div>
    </div>

    <!-- 效期调整按钮 -->
    <div class="action-buttons">
      <button 
        @click="openDateModal" 
        class="modify-date-btn"
        :disabled="!product.productionDate"
      >
        📅 效期调整
      </button>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue';

const emit = defineEmits(['openDateModal']);

const props = defineProps({
  product: {
    type: Object,
    required: true,
    default: () => ({
      id: '',
      productImage: '',
      productName: '',
      storageCondition: '',
      supplier: '',
      barcode: '',
      productionDate: '',
      availableStock: 0,
      lockedStock: 0,
      totalStock: 0,
      locationCode: ''
    })
  }
});

// 格式化日期
function formatDate(dateString) {
  if (!dateString) return '-';
  
  try {
    const date = new Date(dateString);
    return date.toLocaleDateString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit'
    });
  } catch (error) {
    return dateString;
  }
}

// 打开生产日期修改弹窗
function openDateModal() {
  emit('openDateModal', props.product);
}

// 图片加载失败处理
function onImageError(event) {
  event.target.style.display = 'none';
  event.target.nextElementSibling?.classList.remove('hidden');
}
</script>

<style scoped>
/* 参考项目风格的商品卡片 */
.product-card {
  background: #fff;
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(0, 0, 0, 0.06);
  transition: all 0.2s ease;
}

.product-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
}

/* 商品名称 */
.product-name {
  font-size: 18px;
  font-weight: 800;
  color: #000000;
  margin-bottom: 12px;
  line-height: 1.4;
}

/* 商品基本信息 */
.product-meta {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.meta-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.meta-label {
  width: 90px;
  font-size: 14px;
  color: #000000;
  font-weight: 500;
}

.meta-value {
  font-size: 14px;
  color: #000000;
  font-weight: 600;
  flex: 1;
  word-break: break-all;
}

/* 库存统计区域 */
.stock-stats {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.stat-item {
  flex: 1;
  min-width: 80px;
  text-align: center;
  padding: 12px 8px;
  border-radius: 8px;
  border: 2px solid;
}

.stat-item.available {
  background: #d4edda;
  border-color: #28a745;
  color: #155724;
}

.stat-item.locked {
  background: #fff3cd;
  border-color: #ffc107;
  color: #856404;
}

.stat-item.total {
  background: #d1ecf1;
  border-color: #17a2b8;
  color: #0c5460;
}

.stat-label {
  display: block;
  font-size: 12px;
  margin-bottom: 4px;
  font-weight: 500;
}

.stat-number {
  display: block;
  font-size: 18px;
  font-weight: 700;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .product-card {
    padding: 14px;
    border-radius: 14px;
    margin-bottom: 12px;
  }
  
  .product-name {
    font-size: 16px;
    margin-bottom: 10px;
    font-weight: 700;
  }
  
  .product-meta {
    gap: 6px;
    margin-bottom: 14px;
  }
  
  .meta-row {
    gap: 8px;
  }
  
  .meta-label {
    width: 75px;
    font-size: 13px;
    font-weight: 600;
  }
  
  .meta-value {
    font-size: 13px;
  }
  
  .stock-stats {
    gap: 6px;
  }
  
  .stat-item {
    padding: 10px 8px;
    border-radius: 10px;
  }
  
  .stat-label {
    font-size: 11px;
    font-weight: 600;
  }
  
  .stat-number {
    font-size: 16px;
  }
  
  .action-buttons {
    margin-top: 14px;
    padding-top: 14px;
  }
  
  .modify-date-btn {
    padding: 10px 16px;
    font-size: 14px;
    border-radius: 10px;
    font-weight: 700;
  }
}

@media (max-width: 480px) {
  .product-card {
    padding: 16px;
    border-radius: 16px;
  }
  
  .product-name {
    font-size: 17px;
    font-weight: 800;
    margin-bottom: 12px;
  }
  
  .meta-row {
    display: grid;
    grid-template-columns: 80px 1fr;
    gap: 10px;
    align-items: center;
    padding: 2px 0;
  }
  
  .meta-label {
    font-size: 13px;
    font-weight: 700;
    width: auto;
  }
  
  .meta-value {
    font-size: 13px;
    font-weight: 600;
  }
  
  .stock-stats {
    flex-direction: column;
    gap: 8px;
  }
  
  .stat-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: left;
    padding: 12px 16px;
    border-radius: 12px;
  }
  
  .stat-label,
  .stat-number {
    display: inline;
    margin: 0;
  }
  
  .stat-label {
    font-size: 13px;
    font-weight: 600;
  }
  
  .stat-number {
    font-size: 18px;
    font-weight: 800;
  }

  .modify-date-btn {
    width: 100%;
    font-size: 15px;
    padding: 14px 16px;
    border-radius: 12px;
    font-weight: 800;
    height: 48px;
  }
}

/* 超小屏幕优化 */
@media (max-width: 360px) {
  .product-card {
    padding: 12px;
  }
  
  .product-name {
    font-size: 16px;
  }
  
  .meta-row {
    grid-template-columns: 70px 1fr;
    gap: 8px;
  }
  
  .meta-label {
    font-size: 12px;
  }
  
  .meta-value {
    font-size: 12px;
  }
  
  .stat-item {
    padding: 10px 12px;
  }
  
  .stat-label {
    font-size: 12px;
  }
  
  .stat-number {
    font-size: 16px;
  }
}

/* 操作按钮区域 */
.action-buttons {
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid rgba(0, 0, 0, 0.06);
  display: flex;
  justify-content: center;
}

.modify-date-btn {
  background: #f8b300;
  color: #000000;
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  gap: 6px;
}

.modify-date-btn:hover:not(:disabled) {
  background: #e0a200;
  transform: translateY(-1px);
}

.modify-date-btn:disabled {
  background: #cccccc;
  color: #999999;
  cursor: not-allowed;
  transform: none;
}

</style>